//
//  DiscoverViewController.m
//  01 微博
//
//  Created by zhoup on 16/5/14.
//  Copyright © 2016年 zhoup. All rights reserved.
//

#import "DiscoverViewController.h"
#import "UIView+Frame.h"

@interface DiscoverViewController ()<UITextFieldDelegate>

@property(nonatomic,strong) UITextField *textField;

@end

@implementation DiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITextField *textField = [[UITextField alloc]init];
    //默认是没有边框，设置圆角边框
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.frame = CGRectMake(0, 0, 300, 30);
    //设置左边的图标
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"] ];
    icon.width = 30;
    icon.contentMode = UIViewContentModeCenter;
    textField.leftView = icon;
    //左边图标默认不显示，需要设置
    textField.leftViewMode = UITextFieldViewModeAlways;
    //设置清除按钮
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    textField.delegate =  self;
    self.textField =  textField;
    self.navigationItem.titleView = textField;
    
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancel)];
    
    UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"settings_statistic_triangle"]];
     icon.width = 30;
    //图标居中
    icon.contentMode = UIViewContentModeCenter;
    //更换图标
   
    self.textField.leftView = icon;
    
    return YES;
}

-(void)cancel
{
    //取消显示
    self.navigationItem.rightBarButtonItem = nil;
    //关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    //设置左边的图标
    self.textField.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"]];
}

// 拖动表格 关闭键盘
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
